# -*- coding: utf-8 -*-
"""
Created on Wed Apr 24 10:33:14 2024

@author: 刘翼
"""

import cv2
import numpy as np
from matplotlib import pyplot as plt
# 读取两幅图像
img1 = cv2.imdecode(np.fromfile(r"C:\Users\Public\opencv\Figure\adidas.jpg",dtype=np.uint8),1)
img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2RGB)
img2 = cv2.imdecode(np.fromfile(r"C:\Users\Public\opencv\Figure\adidas_logo.jpg",dtype=np.uint8),1)
img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2RGB)
# 创建ORB特征检测器和描述符
orb = cv2.ORB_create()
# 对两幅图像检测特征和描述符
kp1, des1 = orb.detectAndCompute(img1,None)
kp2, des2 = orb.detectAndCompute(img2,None) #找到关键点
# 获得一个暴力匹配器的对象
bf = cv2.BFMatcher(normType=cv2.NORM_HAMMING, crossCheck=True)
# 利用匹配器匹配两个描述符的相近程度
matches = bf.match(des1,des2)
# 按照相近程度 进行排序
matches = sorted(matches, key = lambda x:x.distance)
# 画出匹配项，使用plt将两个图像的匹配结果显示出来
img3 = cv2.drawMatches(img1=img1,keypoints1=kp1,img2=img2,
keypoints2=kp2, matches1to2=matches, outImg=img2, flags=2)
plt.imshow(img3) ,plt.axis('off')
plt.show()
